# -*- coding: utf-8 -*-
import cgi, os, sys
from google.appengine.ext.webapp import template, \
        WSGIApplication
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.api import users

class Error404(webapp.RequestHandler):
    def get(self):
        path = os.path.join(os.path.dirname(__file__), 'views/admin/404.html')
        self.response.out.write(template.render(path, ''))


class admin_main(webapp.RequestHandler):
    def get(self):
        if users.is_current_user_admin():
            path = os.path.join(os.path.dirname(__file__), 'views/admin/index.html')
            self.response.out.write(template.render(path, ''))
        else:
            self.redirect('/post.html')

class admin_entry(webapp.RequestHandler):
    def __init__(self):
        pass
    
    def get(self, slug='post'):
        path = os.path.join(os.path.dirname(__file__), 'views/admin/entry.html')
        self.response.out.write(template.render(path, ''))

def main():
    application = webapp.WSGIApplication(
                                [
                                ('/admin/{0,1}', admin_main),
                                ('/admin/(post|page)', admin_entry),
                                ('/.*', Error404),
                                ], debug=True)
    run_wsgi_app(application)

if __name__ == "__main__":
    main()
